package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *批量处理预约设置
 * Author chongge
 * Time 2019/10/28 15:16
 * ClassName com.itheima.controller
 */
@RestController
@RequestMapping(value = "/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public Result upload(@RequestParam("excelFile")MultipartFile multipartFile){
        try {
            //获取Excel文件，调用POI工具 得到List<String[]>
            List<String[]> list = POIUtils.readExcel(multipartFile);
            //将list转为List<Ordersetting>
            if(list != null && list.size() > 0){
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting();
                    //date对象存入
                    orderSetting.setOrderDate(new Date(strings[0]));
                    //数量存入
                    orderSetting.setNumber(Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                //调用service保存数据库
                orderSettingService.save(orderSettingList);

            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.UPLOAD_SUCCESS);
    }


    /***
     * 根据年月查询预约数据并回显
     * @return
     */
    @RequestMapping(value = "getOrderSettingByMonth",method = RequestMethod.GET)
    public Result getOrderSettingByMonth(String date){

        try {
            List<Map> mapList = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    /***
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping(value = "editNumberByDate",method = RequestMethod.POST)
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            System.out.println(orderSetting);
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,  MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,  MessageConstant.ORDERSETTING_FAIL);
        }


    }
}
